package com.live.adapter;

import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.live.activity.LiveFriendHomeActivity;
import com.live.activity.R;
import com.live.common.Constant;
import com.live.common.ImageDownloader;
import com.live.task.LiveTopic;

public class LiveNewsSA extends SimpleAdapter {

	public List<Map<String, Object>> list = null;
	public Context context = null;
	public String userId = null;
	public List<Map<String, Object>> items;

	private final ImageDownloader imageDownloader = new ImageDownloader();

	public LiveNewsSA(Context context, List<Map<String, Object>> items, int resource, String[] from, int[] to,
			String userId) {
		super(context, items, resource, from, to);
		this.list = items;
		this.context = context;
		this.userId = userId;
		this.items = items;
	}

	@Override
	@SuppressWarnings("unchecked")
	public View getView(final int position, View convertView, ViewGroup parent) {

		if (convertView == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			convertView = inflater.inflate(R.layout.topic_list, null, true);
		}
		if (position <= list.size()) {
			final Map<String, Object> obj = (Map<String, Object>) getItem(position);

			// 设置内容
			TextView content = (TextView) convertView.findViewById(R.id.content);
			if (obj.get("title") != null) {
				content.setText(Html.fromHtml("<font color='#2D86B0'>" + obj.get("userId") + "</font>" + " "
						+ obj.get("content") + " " + "<font color='#2D86B0'>" + obj.get("title") + "</font>"));
			} else {
				content.setText(Html.fromHtml("<font color='#2D86B0'>" + obj.get("userId") + "</font> "
						+ obj.get("content")));
			}

			// 设置城市
			if (!"null".equals(obj.get("userCity").toString())) {
				TextView userCity = (TextView) convertView.findViewById(R.id.userCity);
				userCity.setText((String) obj.get("userCity"));
			}

			// 设置性别
			TextView userSex = (TextView) convertView.findViewById(R.id.userSex);
			if (Constant.USER_SEX_MAN.equals(obj.get("userSex"))) {
				userSex.setText("♂");
				userSex.setTextColor(0xff00CCFF);
			} else {
				userSex.setText("♀");
				userSex.setTextColor(0xffFF6699);
			}

			// 设置年龄
			TextView userAge = (TextView) convertView.findViewById(R.id.userAge);
			userAge.setText((String) obj.get("userAge"));

			// 设置回复数
			TextView replayCount = (TextView) convertView.findViewById(R.id.replayCount);
			replayCount.setText((String) obj.get("rCount"));

			// 设置日期
			TextView topicDate = (TextView) convertView.findViewById(R.id.topicDate);
			topicDate.setText((String) obj.get("updateDate"));

			// 设置头像
			ImageView userPic = (ImageView) convertView.findViewById(R.id.userPic);
			if (Constant.Y.equals(obj.get("userPic"))) {
				String imageUrl = Constant.SITE_URL + Constant.SITE_AVATAR_PATH + obj.get("uId") + ".jpg";
				imageDownloader.download(imageUrl, userPic, (String) obj.get("userPicVer"));
			} else {
				userPic.setImageResource(R.drawable.default_pic);
			}
			// 禁止自Activity循环调用
			if (LiveFriendHomeActivity.class != ((Activity) context).getClass()) {
				userPic.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						// 当不是本人的时候
						if (!userId.equals((String) obj.get("userId"))) {
							Intent intent = new Intent();
							intent.setClass(context, LiveFriendHomeActivity.class);
							Bundle bundle = new Bundle();
							bundle.putString("f", (String) obj.get("f"));
							bundle.putString("uId", (String) obj.get("uId"));
							bundle.putString("userId", (String) obj.get("userId"));
							bundle.putString("userSign", (String) obj.get("userSign"));
							bundle.putString("userSex", (String) obj.get("userSex"));
							bundle.putString("userAge", (String) obj.get("userAge"));
							bundle.putString("userCity", (String) obj.get("userCity"));
							bundle.putString("userPic", (String) obj.get("userPic"));
							bundle.putString("userPicVer", (String) obj.get("userPicVer"));
							intent.putExtras(bundle);
							context.startActivity(intent);
						}
					}
				});
			}

			// 设置删除按钮
			ImageView delBtn = (ImageView) convertView.findViewById(R.id.delBtn);
			if (obj.get("userId").equals(userId)) {
				delBtn.setVisibility(View.VISIBLE);
				// 删除评论事件监听
				delBtn.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {

						// 生成确认对话框
						AlertDialog.Builder builder = new AlertDialog.Builder(context);
						builder.setTitle(R.string.delete_confirm_info);
						builder.setIcon(android.R.drawable.ic_dialog_info);
						builder.setPositiveButton(R.string.delete_btn, new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog, int which) {

								Bundle bundle = new Bundle();
								bundle.putString("id", (String) obj.get("id"));
								bundle.putInt("index", position);

								// 删除评论
								LiveTopic task = new LiveTopic(context, items, LiveNewsSA.this, null,
										Constant.ACTION_DELETE, null, null);
								task.execute(bundle);

							}
						});
						builder.setNegativeButton(R.string.cancel_btn, new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog, int which) {
							}
						});
						builder.create().show();

					}
				});
			} else {
				delBtn.setVisibility(View.INVISIBLE);
			}
		}
		return convertView;
	}

	public ImageDownloader getImageDownloader() {
		return imageDownloader;
	}
}